var colorArrayLen = 7;

function changePlayerInputs(selectObject){
	//alert(selectObject.value);
	var pCount = selectObject.value;
	for (var i=2;i<=4;i++){
		if(i <= pCount){
			document.getElementById("p"+ i).style.display="block";
			document.getElementById("phrs_"+ (i-1)).style.display="block";
		}
		else{
			document.getElementById("p"+ i).style.display="none";
			document.getElementById("phrs_"+ (i-1)).style.display="none";
			document.getElementById("p"+ i+ "name").value=null;
			document.getElementById("p"+ i+ "sname").value=null;
			
			for(var j=1;j<=colorArrayLen;j++){
				document.getElementById("p"+ i + "" + j + "color").checked=false;
			}
		}
	}
}

//ctxRoot defined in the jsp file importing the js script file
function createProfile(){
	
	var retVal = buildNewGameQueryString();
//	alert(retVal);
	if(retVal == false)
		return;

	var loaderPannel = document.getElementById("loaderPannel");
	loaderPannel.style.display="block";

	var mainFrm = document.getElementById("mainFrameWork");
	
	mainFrm.style.filter="alpha(opacity=25)";
	mainFrm.style.opacity="0.25";
	document.body.style.backgroundColor="#EEEEEE";

	xmlHttpPost(ctxRoot + "/servlet/PlayerBot",retVal,"PLAYER");
}

function buildNewGameQueryString(){
	var qryStr="";
	
	var playerCountObj = document.getElementById("playercount");
	var gameLevelObj = document.getElementById("gamelevel");
	var gameNameObj = document.getElementById("gamename");
	
	var playerCount = playerCountObj.options[playerCountObj.selectedIndex].value;
	var gameLevel = gameLevelObj.options[gameLevelObj.selectedIndex].value;
	var gameName = gameNameObj.value;
	
	//alert("Playing " + gameLevel + " with " + playerCount + " players");
	for(var i=1;i<=playerCount; i++){
		var name = document.getElementById("p" + i +"name");
		var sname = document.getElementById("p" + i +"sname");
		var color;
		for(var j=1;j<=colorArrayLen;j++){
			color = document.getElementById("p"+ i + "" + j + "color");
			if(color.checked == true)
				break;
			else
				color = null;
		}
		
		if(!name || !sname || !color){
			alert("Missing player data");
			return false;
		}
		qryStr = qryStr + "Player" + i +"Name=" + name.value + "&";
		qryStr = qryStr + "Player" + i +"SName=" + sname.value + "&";
		qryStr = qryStr + "Player" + i +"Color=" + color.value + "&";
	}
	
	qryStr = qryStr + "PlayerCount="  + playerCount + "&";
	qryStr = qryStr + "GameLevel=" + gameLevel + "&";
	qryStr = qryStr + "GameName=" + gameName + "&";
	
	qryStr = qryStr + "Command=NewGame";
	return qryStr;
}

function joinGame(gameRoom){
	
	var retVal = buildJoinGameQueryString();
//	alert(retVal);
	if(retVal == false)
		return;

	var loaderPannel = document.getElementById("loaderPannel");
	loaderPannel.style.display="block";

	var mainFrm = document.getElementById("mainFrameWork");
	
	mainFrm.style.filter="alpha(opacity=25)";
	mainFrm.style.opacity="0.25";
	document.body.style.backgroundColor="#EEEEEE";

	xmlHttpPost(ctxRoot + "/servlet/PlayerBot",retVal,"PLAYER");
}

function buildJoinGameQueryString(){
	var qryStr="";
	
	var playerCountObj = document.getElementById("playercount");
	
	var playerCount = playerCountObj.options[playerCountObj.selectedIndex].value;
	
	//alert("Playing " + gameLevel + " with " + playerCount + " players");
	for(var i=1;i<=playerCount; i++){
		var name = document.getElementById("p" + i +"name");
		var sname = document.getElementById("p" + i +"sname");
		var color;
		for(var j=1;j<=colorArrayLen;j++){
			color = document.getElementById("p"+ i + "" + j + "color");
			if(color.checked == true)
				break;
			else
				color = null;
		}
		
		if(!name || !sname || !color){
			alert("Missing player data");
			return false;
		}
		qryStr = qryStr + "Player" + i +"Name=" + name.value + "&";
		qryStr = qryStr + "Player" + i +"SName=" + sname.value + "&";
		qryStr = qryStr + "Player" + i +"Color=" + color.value + "&";
	}
	
	qryStr = qryStr + "PlayerCount="  + playerCount + "&";
	qryStr = qryStr + "GameLevel=" + gameLevel + "&";
	qryStr = qryStr + "GameName=" + gameName + "&";
	
	qryStr = qryStr + "Command=NewGame";
	return qryStr;
}

function updatepage(){
	for(var i=0;i<arguments.length;i++){
//		alert("arguments["+i+"]="+ arguments[i]);
	}
	document.getElementById("loaderPannel_wait").style.display="none";
	document.getElementById("loaderPannel_resp").style.display="block";
	document.getElementById("loaderPannel_resptxt").innerHTML=arguments[0];
}

function ackServerResp(){
	var loaderPannel = document.getElementById("loaderPannel");
	loaderPannel.style.display="none";

	document.getElementById("loaderPannel_wait").style.display="block";
	document.getElementById("loaderPannel_resp").style.display="none";
	document.getElementById("loaderPannel_resptxt").innerHTML=null;

	var mainFrm = document.getElementById("mainFrameWork");
	mainFrm.style.filter="alpha(opacity=100)";
	mainFrm.style.opacity="1";
	document.body.style.backgroundColor="Window";
}